package com.cskaoyan.bean.data;

import com.cskaoyan.model.MarketCart;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.util.List;
import java.util.stream.IntStream;

/**
 * @Description
 * @Author zoe ling
 * @Date 2024/6/12
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CartStat {
    private Integer goodsCount;
    private Integer checkedGoodsCount;
    private BigDecimal goodsAmount;
    private BigDecimal checkedGoodsAmount;

    public static CartStat stat( List<MarketCart> cartList){
        //将cartlist中的number进行累加
        int goodsCount = cartList.stream().mapToInt(MarketCart::getNumber).sum();
        //挑选出checked=true的部分的number进行累加
        int checkedGoodsCount = cartList.stream().filter(MarketCart::getChecked).mapToInt(MarketCart::getNumber).sum();
        //计算每个订单的price和number的乘积（当前订单总价），然后累加
        BigDecimal goodsAmount = cartList.stream().map(item -> {
            BigDecimal price = item.getPrice();
            int number = item.getNumber();
            return price.multiply(BigDecimal.valueOf(number));
        }).reduce(BigDecimal.ZERO, BigDecimal::add);
        //挑选出checked=true的部分订单进行累加
        BigDecimal checkedGoodsAmount = cartList.stream().filter(MarketCart::getChecked).map(item -> {
            BigDecimal price = item.getPrice();
            int number = item.getNumber();
            return price.multiply(BigDecimal.valueOf(number));
        }).reduce(BigDecimal.ZERO, BigDecimal::add);

        return CartStat.builder()
                .goodsCount(goodsCount)
                .checkedGoodsCount(checkedGoodsCount)
                .goodsAmount(goodsAmount)
                .checkedGoodsAmount(checkedGoodsAmount)
                .build();
    }
}
